package com.weijun.keepalive_daemon

import android.app.Activity
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.PowerManager
import android.os.Process
import android.util.Log
import java.io.File
import java.io.IOException

/**
 * @author: weijun
 * date: 2023/7/6
 * description:
 */
internal const val TAG = "wj_daemon"
internal const val DaemonServicePath = "com.weijun.keepalive_daemon.service.DaemonService"
internal const val CoreServicePath = "com.weijun.keepalive_daemon.service.CoreService"

internal fun String.logE() {
    Log.e(TAG,this)
}

fun Context.isScreenOn():Boolean {
    return try {
        (applicationContext.getSystemService(Activity.POWER_SERVICE) as PowerManager).isScreenOn
    } catch (ignore: Exception) {
        true
    }
}

@Throws(IOException::class)
internal infix fun File.createNewFile(fileName:String) {
    val file = File(this, fileName)
    if (!file.exists()) {
        file.createNewFile()
    }
}

fun Application.isMainProcess():Boolean {
    var processName = ""
    val mActivityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    if (mActivityManager.runningAppProcesses != null) {
        for (appProcess in mActivityManager.runningAppProcesses) {
            if (appProcess.pid == Process.myPid()) {
                processName = appProcess.processName
                break
            }
        }
        if (processName == packageName) {
            return true
        }
    }
    return false
}